﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI.Skins;
using Telerik.Web.UI;
using Newtonsoft.Json;
using Fly.Common.Linq;
using RN.ERP.Web.ModelService;
using RN.ERP.Web.PersonService;
using RN.ERP.Web.BasicService;
using System.Web.UI.HtmlControls;
namespace RN.ERP.Web.InfoChange
{
    public partial class ChangeSetRecordList : System.Web.UI.Page
    {
        public string FlowCode
        {
            get
            {
                return this.Request.QueryString["flowcode"] == null ? string.Empty : this.Request.QueryString["flowcode"].ToString();
            }
        }
        BasicServiceClient basicServiceClient = new BasicServiceClient();
        PersonServiceClient personClient = new PersonServiceClient();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindFlowCode();
            }
        }

        protected void ListChangeSet_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            var queryCondition = hdnCondition.Value;
            if (string.IsNullOrEmpty(queryCondition))
            {
                queryCondition = GetCondition();
            }
            int pageSize = ListChangeSet.PageSize;
            int pageIndex = ListChangeSet.CurrentPageIndex + 1;
            int count = 0;
            var changesets = basicServiceClient.GetChangeSetRecordList(out count,pageIndex, pageSize, queryCondition, "00");
            var list = JsonConvert.DeserializeObject<IList<ChangeDetail>>(changesets).ToList();
            int cnt = count;
            ListChangeSet.VirtualItemCount = cnt;
            ListChangeSet.DataSource = list;
        }

        ////获取查询条件
        private string GetCondition()
        {
            List<string> listFlow = new List<string>();
            List<QueryPersonInfoCondition> queryList = new List<QueryPersonInfoCondition>();
            foreach (var f in listFlow)
            {
                QueryPersonInfoCondition condition = new QueryPersonInfoCondition();
                string title = txtTitle.Text.Trim();
                if (!string.IsNullOrEmpty(title))
                {
                    condition.Name = new ParameterValue<string>() { O = OptEnum.Like, V = title };
                }
                condition.FlowCode = new ParameterValue<string>() { O = OptEnum.Equal, V = f };
                queryList.Add(condition);
            }
            return JsonConvert.SerializeObject(queryList);
        }

        /// <summary>
        /// 客户端grid刷新
        /// </summary>
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            switch (e.Argument)
            {
                case "Rebind":
                    ListChangeSet.MasterTableView.SortExpressions.Clear();
                    ListChangeSet.MasterTableView.GroupByExpressions.Clear();
                    ListChangeSet.Rebind();
                    break;
                case "Audit":
                    break;
                case "Back":
                    break;
                case "UnlockBack":
                    break;
                default:
                    break;
            }
        }

        public string GetChangeInfo(object list)
        {
            IList<Tb_ChangeRecord> listrecord = (IList<Tb_ChangeRecord>)list;
            return "test";         
        }
        protected void ToggleRowSelection(object sender, EventArgs e)
        {
            ((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
            bool checkHeader = true;
            foreach (GridDataItem dataItem in ListChangeSet.MasterTableView.Items)
            {
                if (!(dataItem.FindControl("CheckBox1") as CheckBox).Checked)
                {
                    checkHeader = false;
                    break;
                }
            }
            GridHeaderItem headerItem = ListChangeSet.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
            (headerItem.FindControl("headerChkbox") as CheckBox).Checked = checkHeader;
        }

        protected void ToggleSelectedState(object sender, EventArgs e)
        {
            CheckBox headerCheckBox = (sender as CheckBox);
            foreach (GridDataItem dataItem in ListChangeSet.MasterTableView.Items)
            {
                (dataItem.FindControl("CheckBox1") as CheckBox).Checked = headerCheckBox.Checked;
                dataItem.Selected = headerCheckBox.Checked;
            }
        }

        private void BindFlowCode()
        {
           
        }

        //public override bool IsCheckPermission
        //{
        //    get { return false; }
        //}

        /// <summary>
        /// 只显示N个字的方法
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string StrCut(string str, int num)
        {
            string nstr = System.Text.RegularExpressions.Regex.Replace(str, @"<[^>]*>", "");
            nstr = nstr.Replace("&nbsp;<STRONG></STRONG>", "");
            if (nstr.Length >= num)
            {
                return nstr.Substring(0, num) + "...";
            }
            return nstr;
        }

        protected void RadToolBar1_Load(object sender, EventArgs e)
        {
            Telerik.Web.UI.RadToolBar toolbar = (Telerik.Web.UI.RadToolBar)sender;
            RadToolBarItem barItem = toolbar.FindItemByText("解锁退回");
            RadToolBarButton radButton = (RadToolBarButton)barItem;
        }

        protected void ListChangeSet_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                var currentItem = e.Item as GridDataItem;
                ChangeDetail changeDetail = (ChangeDetail)currentItem.DataItem;
                var table = (Table)currentItem.FindControl("ChangeTable");
                foreach(var item in changeDetail.ChangeInfos)
                {
                    TableRow row = new TableRow();
                    TableCell cellBefore = new TableCell();
                    cellBefore.Width = 100;
                    cellBefore.Text = item.BeforeChange;
                    cellBefore.CssClass = "NoNewline";
                    row.Cells.Add(cellBefore);
                    TableCell cellAfter = new TableCell();
                    cellAfter.Width = 100;
                    cellAfter.Text = item.AfterChange;
                    cellAfter.CssClass = "NoNewline";
                    row.Cells.Add(cellAfter);
                    table.Rows.Add(row);
                }      
            }
        }
    }
}